package com.example.admin.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.admin.R;
import com.example.admin.SQLite.Complaint;
import com.example.admin.SQLite.SqLiteOpenHelper;
import com.example.admin.SQLite.YiJian;
import com.example.admin.SQLite.dao.YiJianDao;
import com.example.admin.adapter.AdminComplaintAdapter;

import java.util.ArrayList;
import java.util.List;

public class Tab4Fragment extends Fragment {
    TextView Tab1, Tab2, Tab3,tvTimes;
    YiJianDao yiJianDao;
    AdminComplaintAdapter mAdapter;
    RecyclerView recyclerView;
    int poin=1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_tab4, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        yiJianDao = new YiJianDao(getActivity());
        recyclerView = view.findViewById(R.id.recyclerView);
        tvTimes = view.findViewById(R.id.tvTimes);
        Tab1 = view.findViewById(R.id.Tab1);
        Tab2 = view.findViewById(R.id.Tab2);
        Tab3 = view.findViewById(R.id.Tab3);
        mAdapter = new AdminComplaintAdapter();
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("是否执行此操作");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        YiJian bean = mAdapter.getItem(position);
                        yiJianDao.updateCom(bean.id, bean.yijianState.equals("待处理")?"处理中":bean.yijianState.equals("处理中")?"已处理":"已处理");
                        getData(poin);
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();

            }
        });
        getData(poin);
        Tab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                poin=1;
                getData(poin);
                tvTimes.setText("发表时间");
            }
        });
        Tab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                poin=2;
                getData(poin);
                tvTimes.setText("确定处理时间");

            }
        });
        Tab3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                poin=3;
                getData(poin);
                tvTimes.setText("完成处理时间");

            }
        });
    }

    private void getData(int poin) {
        List<YiJian> data = new ArrayList<>();
        List<YiJian> list = yiJianDao.fetchData();
        Log.e("的方法士大夫", "getData: "+list.toString());
        switch (poin) {
            case 1:
                for (YiJian bean : list) {
                    if (bean.yijianState.equals("待处理")) {
                        data.add(bean);
                    }
                }

                break;
            case 2:
                for (YiJian bean : list) {
                    if (bean.yijianState.equals("处理中")) {
                        data.add(bean);
                    }
                }

                break;
            case 3:
                for (YiJian bean : list) {
                    if (bean.yijianState.equals("已处理")) {
                        data.add(bean);
                    }
                }

                break;
        }
        mAdapter.setNewData(data);
    }
}